package cn.tedu.tnt_sky.common.utils;

import cn.tedu.tnt_sky.common.response.ALIBABA_CLOUD_ACCESS;
import com.aliyun.tea.TeaException;

/**
 * @author Jason Wen
 * @version 1.0
 *
 * 短信验证码
 */
public class Sample {
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 发送验证码
     * @param phone 电话
     * @param verificationCode 验证码
     */
    public static void sendingCode (String phone, String verificationCode){
        com.aliyun.dysmsapi20170525.Client client = null;
        try {
            client = Sample.createClient(ALIBABA_CLOUD_ACCESS.ALIBABA_CLOUD_ACCESS_KEY_ID, ALIBABA_CLOUD_ACCESS.ALIBABA_CLOUD_ACCESS_KEY_SECRET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName("TntbokeWeb")
                .setTemplateCode("SMS_462750064")
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":\"" + verificationCode + "\"}");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            client.sendSmsWithOptions(sendSmsRequest, runtime);
        } catch (TeaException error) {
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}